package io.renren.modules.bonus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.page.PageData;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.bonus.dao.BonusTypeDao;
import io.renren.modules.bonus.vo.BonusTypeVO;
import io.renren.modules.bonus.dto.BonusTypeDTO;
import io.renren.modules.bonus.entity.BonusTypeEntity;
import io.renren.modules.bonus.service.BonusTypeService;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * 奖金类型
 *
 * @author test test
 * @since 1.0.0 2024-09-10
 */
@Service
public class BonusTypeServiceImpl extends CrudServiceImpl<BonusTypeDao, BonusTypeEntity, BonusTypeVO> implements BonusTypeService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public QueryWrapper<BonusTypeEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String typeName = (String)params.get("typeName");

        QueryWrapper<BonusTypeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.like(StringUtils.isNotBlank(typeName), "type_name", typeName);

        return wrapper;
    }

    @Override
    public void save(BonusTypeDTO bonusTypeDTO) {
        BonusTypeEntity bonusTypeEntity = ConvertUtils.sourceToTarget(bonusTypeDTO, BonusTypeEntity.class);
        this.insert(bonusTypeEntity);
    }

    @Override
    public void update(BonusTypeDTO bonusTypeDTO) {
        BonusTypeEntity bonusTypeEntity = ConvertUtils.sourceToTarget(bonusTypeDTO, BonusTypeEntity.class);
        this.updateById(bonusTypeEntity);
    }

    @Override
    public List<BonusTypeVO> getAll() {
        List<BonusTypeEntity> entityList = this.baseDao.selectList(null);
        return ConvertUtils.sourceToTarget(entityList, BonusTypeVO.class);
    }

    @Override
    public PageData<BonusTypeVO> bonustypePage(Map<String, Object> params) {
        List<SysUserDTO> userDtoList = sysUserService.getAll();
        PageData<BonusTypeVO> page = this.page(params);
        page.getList().forEach(e -> {
            SysUserDTO creator = userDtoList.stream().filter(j -> j.getId().compareTo(e.getCreator()) == 0).findAny().orElse(null);
            e.setCreatorName(creator == null ? "-" : creator.getRealName());
            SysUserDTO updater = userDtoList.stream().filter(j -> j.getId().compareTo(e.getUpdater()) == 0).findAny().orElse(null);
            e.setUpdaterName(updater == null ? "-" : updater.getRealName());
        });
        return page;
    }


}